<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search
      @search="reload"
      :where="defaultWhere"
      v-if="search"
      :badge-data="badgeData"
    />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        :where="defaultWhere"
      >
        <template #toolbar>
          <el-button
            v-if="props.action == 'list'"
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>

        <template #status="{ row }">
          {{ row.id }}<br />
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>

        <template #belongs_date="{ row }">
          <span class="dark">{{ row.id }}</span> <br />
          {{ row.belongsDate }}
        </template>
        <template #account="{ row }">
          {{ row.payAccountType }}<br />
          {{ row.pay_account_realname }}<br />
          {{ row.pay_account }}
        </template>

        <template #company="{ row }">
          {{ row.company }} <br />
          {{ row.position }}
        </template>

        <template #amount="{ row }">
          {{ row.amount }} <br />
          {{ row.remark }}<br />
          <span class="pointer blue" @click="openImages(row.imageList)">
            查看凭证
          </span>
        </template>

        <template #judgeTimes="{ row }">
          <span class="pointer blue" @click="openJudgeLogs(row)">
            {{ row.judge_times }}
          </span>
        </template>

        <template #create="{ row }">
          {{ row.create_user }} <br />
          {{ row.create_time }}
        </template>

        <template #done="{ row }">
          {{ row.done_user }} <br />
          {{ row.done_time }} <br />
          <span class="pointer blue" @click="openImages(row.payImages)">
            查看凭证
          </span>
        </template>

        <template #reject="{ row }">
          {{ row.reject_user }} <br />
          {{ row.reject_time }}
        </template>

        <template #action="{ row }">
          <el-button-group>
            <el-button
              type="info"
              size="small"
              @click="openEdit(row)"
              v-if="props.action == 'list'"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="del(row)"
              v-if="props.action == 'list'"
            >
              删除
            </el-button>
            <el-button
              type="success"
              size="small"
              @click="setStatus(row, row.status)"
              v-if="props.action == 'judge' && row.status < 190"
            >
              通过
            </el-button>
            <el-button
              type="success"
              size="small"
              @click="openPay(row)"
              v-if="props.action == 'judge' && row.status == 190"
            >
              打款
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="setStatus(row, 500)"
              v-if="props.action == 'judge'"
            >
              拒绝
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <Edit v-model="showEdit" :data="current" @done="reload" />
    <Images v-model="showImages" :data="current" />
    <Pay v-model="showPay" :data="current" @done="payDone" />
    <Logs v-model="showJudgeLogs" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import Search from './search.vue';
  import Edit from './edit.vue';
  import Logs from './logs.vue';
  import Images from './images.vue';
  import Pay from './pay.vue';
  import {
    pageCompanyCost,
    delCompanyCost,
    setCompanyCostStatus
  } from '@/api/company';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';

  const props = defineProps({
    search: {
      type: Boolean,
      default: true
    },
    action: {
      type: String,
      default: 'list'
    },
    companyId: {
      type: Number,
      default: 0
    },
    siteId: {
      type: Number,
      default: 0
    },
    tenantId: {
      type: Number,
      default: 0
    },
    belongsDate: {
      type: String,
      default: ''
    },
    actionStatus: Number
  });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];
    cols.push(
      {
        slot: 'belongs_date',
        label: '所属月份',
        width: 115,
        align: 'center'
      },
      {
        prop: 'cate',
        label: '分类',
        width: 100
      },

      {
        slot: 'company',
        label: '所属项目',
        width: 220
      },

      {
        slot: 'amount',
        label: '费用说明'
      },
      {
        slot: 'account',
        label: '收款账号',
        width: 250,
        align: 'center'
      },
      {
        slot: 'create',
        label: '申请人',
        width: 120
      },
      {
        slot: 'judgeTimes',
        label: '审核次数',
        width: 95,
        align: 'center'
      }
    );

    if (status.value == 500) {
      cols.push({
        label: '拒绝人',
        width: 120,
        slot: 'reject',
        fixed: 'right'
      });
    }

    if (status.value == 200) {
      cols.push({
        label: '打款人',
        width: 120,
        slot: 'done',
        fixed: 'right'
      });
    }

    if (status.value < 200) {
      cols.push({
        columnKey: 'action',
        label: '操作',
        width: 140,
        slot: 'action',
        align: 'center',
        fixed: 'right'
      });
    }

    return cols;
  });

  const defaultWhere = {
    tenant_id: props.tenantId,
    site_id: props.siteId,
    status: props.actionStatus,
    company_id: props.companyId,
    belongs_date: props.belongsDate
  };

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  const badgeData = ref(
    Array.from({ length: 191 }, (_, index) => ({ [index]: 0 }))
  );
  console.log(badgeData.value[0]);

  /** 表格数据源 */

  const datasource = async ({ page, where, orders }) => {
    const result = await pageCompanyCost({
      ...where,
      ...orders,
      page,
      limit: 99999999
    });
    badgeData.value = result.badgeData;
    return result;
  };

  const showImages = ref(false);
  const openImages = (row) => {
    showImages.value = true;
    console.log(row, '???');
    current.value = row;
  };

  const showJudgeLogs = ref(false);
  const openJudgeLogs = (row) => {
    showJudgeLogs.value = true;
    current.value = row;
  };

  const status = ref(props.actionStatus);
  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    status.value = where ? where.status : 0;
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 删除 */
  const del = (row) => {
    ElMessageBox.confirm(
      '确定要删除“' +
        row.company +
        row.position +
        row.amount +
        '”的开支记录吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        delCompanyCost({ id: row.id })
          .then((msg) => {
            EleMessage.success(msg);
            reload();
          })
          .catch((e) => {
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  /** 打款 */
  const showPay = ref(false);
  const openPay = (row) => {
    current.value = row ?? null;
    showPay.value = true;
  };

  const payDone = () => {
    showPay.value = false;
    reload();
  };

  /** 通过 */
  const setStatus = (row, status = 0) => {
    let action = status == 500 ? '拒绝' : '通过';
    ElMessageBox.confirm(
      action + row.company + row.position + row.amount + '的项目开支?',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading({
          message: '正在处理中..',
          mask: true
        });
        setCompanyCostStatus({
          id: row.id,
          status: status
        })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功!');
            reload();
          })
          .catch(() => {
            loading.close();
          });
      })
      .catch(() => {});
  };
</script>
